package com.testdocs.client;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.place.shared.Place;
import com.testdocs.client.project.create.CreateProjectActivity;
import com.testdocs.client.project.create.CreateProjectPlace;
import com.testdocs.client.project.dashboard.DashboardActivity;
import com.testdocs.client.project.dashboard.DashboardPlace;
import com.testdocs.client.project.initialselect.SelectProjectActivity;
import com.testdocs.client.project.initialselect.SelectProjectPlace;
import com.testdocs.client.signin.CheckSignInActivity;
import com.testdocs.client.signin.SignInActivity;
import com.testdocs.client.signin.SignInPlace;
import com.testdocs.client.signin.context.RequiresSignIn;
import com.testdocs.client.test.group.TestGroupActivity;
import com.testdocs.client.test.group.TestGroupPlace;
import com.testdocs.client.version.list.VersionListActivity;
import com.testdocs.client.version.list.VersionListPlace;

public class AppActivityMapper implements ActivityMapper {
	private ClientFactory clientFactory;

	public AppActivityMapper(ClientFactory clientFactory) {
		super();
		this.clientFactory = clientFactory;
	}

	@Override
	public Activity getActivity(Place place) {
		Activity activity = null;
		if (place instanceof SelectProjectPlace) {
			activity = new SelectProjectActivity(clientFactory);
		} else if (place instanceof CreateProjectPlace) {
			activity = new CreateProjectActivity(clientFactory);
		} else if (place instanceof SignInPlace) {
			activity = new SignInActivity((SignInPlace) place, clientFactory);
		} else if (place instanceof DashboardPlace) {
			activity = new DashboardActivity((DashboardPlace) place, clientFactory);
		} else if (place instanceof VersionListPlace) {
			activity = new VersionListActivity((VersionListPlace) place, clientFactory);
		} else if (place instanceof TestGroupPlace) {
			activity = new TestGroupActivity((TestGroupPlace) place, clientFactory);
		} else {
			activity = null;
		}

		if (place instanceof RequiresSignIn && !clientFactory.getSignInContextHolder().isSignedIn()) {
			activity = new CheckSignInActivity(clientFactory, activity);
		}
		
		return activity;
	}
}